<template>
  <!-- 添加产品对话框 -->
  <el-dialog
    :visible.sync="isVisible"
    title="设备保养"
    width="40%"
    @close="$emit('closeDialog')">
    <el-form
      ref="serviceForm"
      :model="serviceForm"
      :rules="serviceForm_rules"
      label-width="120px">
      <el-form-item label="设备号">
        <el-input v-model="serviceForm.machine_no" :disabled="true"></el-input>
      </el-form-item>

      <el-form-item label="设备名称">
        <el-input v-model="serviceForm.name" :disabled="true"></el-input>
      </el-form-item>

      <el-form-item  label="上次保养时间">
        <el-date-picker
          v-model="serviceForm.operate"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :disabled="true">
        </el-date-picker>
      </el-form-item>

      <el-form-item prop="remind" label="下次保养时间">
        <el-date-picker
          v-model="serviceForm.remind"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :validate-event="false">
        </el-date-picker>
      </el-form-item>       
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="isVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit('serviceForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {deviceMaintain,serviceTime} from '@/api/maintain.js'
export default {
  name: 'ProductDialog',
  data() {
      return {
       isVisible:this.isShow,
        serviceForm: {
         facility_id:"",
         machine_no:"",
         name:"",
         operate:"",
         remind:"",
      },
      serviceForm_rules: {
        remind: [
          {
            required: true,
            message: "请选择下次保养时间",
            trigger: "change",
          },
        ],    
      },
  }
 },
 props:{
    isShow: Boolean,
    dialogRow: Object, 
  },
 mounted() {
   this.serviceForm.facility_id = this.dialogRow.facility_id;
   this.serviceForm.machine_no = this.dialogRow.machine_no;
   this.serviceForm.name = this.dialogRow.name;
   this.getServiceTime()
},
 methods:{
   async getServiceTime(){
     const {data}= await serviceTime(this.serviceForm.facility_id)
     this.serviceForm.operate=data.last_date
     this.serviceForm.remind=data.nest_date
    },
    //提交
     onSubmit(serviceForm){
     this.$refs[serviceForm].validate((async valid=>{
         if(valid){
           const formDatas = new FormData(); 
           formDatas.append('is_maintain',0)
           formDatas.append('facility_id',this.serviceForm.facility_id)
           formDatas.append('remind',this.serviceForm.remind)
           await deviceMaintain(formDatas)
           this.$message.success('提交成功！')
           this.isVisible = false; //关闭对话框
         }
       }))
     },
 }
}
</script>

<style lang = "scss" scoped>

</style>
